package Views;

import Controllers.LocationController;
import Controllers.RouteController;
import Models.Location;
import Models.Route;
import Views.TableModels.LocationTableModel;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.event.ListSelectionEvent;

public class RoutePanel extends ViewAbstract<Route>
{
    public RoutePanel(SearchableTabbedPane parent)
	{
		initComponents();
		this.controller = new RouteController();
		this.tabbedPane = parent;
		locationTable.getSelectionModel().addListSelectionListener(this);
		locationTable.addMouseListener(new java.awt.event.MouseAdapter(){
			@Override
			public void mouseClicked(java.awt.event.MouseEvent e){
				if (e.getClickCount() == 2)
				{
					if(locationTable.getSelectedRowCount() == 1)
					{
						showLocationBActionPerformed(null);
					}
				}
			}
		});
	}

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        routeNoL = new javax.swing.JLabel();
        routeTF = new javax.swing.JTextField();
        locationSP = new javax.swing.JScrollPane();
        locationTable = new javax.swing.JTable();
        searchB = new javax.swing.JButton();
        searchTF = new javax.swing.JTextField();
        sep1 = new javax.swing.JSeparator();
        addLocationB = new javax.swing.JButton();
        removeLocationB = new javax.swing.JButton();
        showLocationB = new javax.swing.JButton();
        Sep2 = new javax.swing.JSeparator();
        showAllB = new javax.swing.JButton();
        Sep3 = new javax.swing.JSeparator();
        newB = new javax.swing.JButton();
        editB = new javax.swing.JButton();
        deleteB = new javax.swing.JButton();
        cancelB = new javax.swing.JButton();
        saveB = new javax.swing.JButton();
        totalTimeL = new javax.swing.JLabel();
        totalTimeTF = new javax.swing.JTextField();
        totalKmL = new javax.swing.JLabel();
        totalKmTF = new javax.swing.JTextField();

        setMaximumSize(new java.awt.Dimension(600, 420));
        setMinimumSize(new java.awt.Dimension(600, 420));
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(600, 420));

        routeNoL.setText("Rute nr.:");
        routeNoL.setName("routeNoL"); // NOI18N

        routeTF.setEnabled(false);
        routeTF.setName("routeTF"); // NOI18N

        locationSP.setName("locationSP"); // NOI18N

        locationTable.setModel(new Views.TableModels.LocationTableModel());
        locationTable.setEnabled(false);
        locationTable.setName("locationTable"); // NOI18N
        locationSP.setViewportView(locationTable);

        searchB.setText("Søg");
        searchB.setName("searchB"); // NOI18N
        searchB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchBActionPerformed(evt);
            }
        });

        searchTF.setMaximumSize(new java.awt.Dimension(200, 20));
        searchTF.setMinimumSize(new java.awt.Dimension(200, 20));
        searchTF.setName("searchTF"); // NOI18N
        searchTF.setPreferredSize(new java.awt.Dimension(200, 20));

        sep1.setName("sep1"); // NOI18N

        addLocationB.setText("Tilføj");
        addLocationB.setEnabled(false);
        addLocationB.setName("addLocationB"); // NOI18N
        addLocationB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addLocationBActionPerformed(evt);
            }
        });

        removeLocationB.setText("Fjern");
        removeLocationB.setEnabled(false);
        removeLocationB.setName("removeLocationB"); // NOI18N
        removeLocationB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeLocationBActionPerformed(evt);
            }
        });

        showLocationB.setText("Vis");
        showLocationB.setEnabled(false);
        showLocationB.setName("showLocationB"); // NOI18N
        showLocationB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showLocationBActionPerformed(evt);
            }
        });

        Sep2.setName("Sep2"); // NOI18N

        showAllB.setText("Vis Alle");
        showAllB.setName("showAllB"); // NOI18N
        showAllB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showAllBActionPerformed(evt);
            }
        });

        Sep3.setOrientation(javax.swing.SwingConstants.VERTICAL);
        Sep3.setName("Sep3"); // NOI18N
        Sep3.setPreferredSize(new java.awt.Dimension(5, 23));

        newB.setText("Ny");
        newB.setName("newB"); // NOI18N
        newB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newBActionPerformed(evt);
            }
        });

        editB.setText("Rediger");
        editB.setEnabled(false);
        editB.setName("editB"); // NOI18N
        editB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editBActionPerformed(evt);
            }
        });

        deleteB.setText("Slet");
        deleteB.setEnabled(false);
        deleteB.setName("deleteB"); // NOI18N
        deleteB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteBActionPerformed(evt);
            }
        });

        cancelB.setText("Annullér");
        cancelB.setEnabled(false);
        cancelB.setName("cancelB"); // NOI18N
        cancelB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBActionPerformed(evt);
            }
        });

        saveB.setText("Gem");
        saveB.setEnabled(false);
        saveB.setName("saveB"); // NOI18N
        saveB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveBActionPerformed(evt);
            }
        });

        totalTimeL.setText("Total tid:");
        totalTimeL.setName("totalTimeL"); // NOI18N

        totalTimeTF.setEnabled(false);
        totalTimeTF.setName("totalTimeTF"); // NOI18N

        totalKmL.setText("Total km:");
        totalKmL.setName("totalKmL"); // NOI18N

        totalKmTF.setEnabled(false);
        totalKmTF.setName("totalKmTF"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(routeNoL)
                            .addComponent(totalTimeL))
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(totalTimeTF, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                            .addComponent(routeTF, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                            .addComponent(totalKmTF, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE))
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(searchTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(searchB))
                            .addComponent(sep1))
                        .addContainerGap())
                    .addComponent(totalKmL)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(locationSP, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(addLocationB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removeLocationB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(showLocationB)
                        .addContainerGap(355, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(Sep2, javax.swing.GroupLayout.DEFAULT_SIZE, 588, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(showAllB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(Sep3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(newB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(editB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(deleteB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
                                .addComponent(cancelB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(saveB)))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(routeNoL)
                    .addComponent(routeTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchB)
                    .addComponent(searchTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(totalTimeL)
                            .addComponent(totalTimeTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(totalKmL)
                            .addComponent(totalKmTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(10, 10, 10)
                        .addComponent(locationSP, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(removeLocationB)
                                .addComponent(showLocationB))
                            .addComponent(addLocationB)))
                    .addComponent(sep1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(67, 67, 67)
                .addComponent(Sep2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(showAllB)
                    .addComponent(Sep3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(newB)
                        .addComponent(editB)
                        .addComponent(saveB)
                        .addComponent(cancelB)
                        .addComponent(deleteB)))
                .addGap(27, 27, 27))
        );
    }// </editor-fold>//GEN-END:initComponents

	private void newBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_newBActionPerformed
	{//GEN-HEADEREND:event_newBActionPerformed
		clearComponents();
		changeComponentState(true);
		newOne = new Route();
		newB.setEnabled(false);
		editB.setEnabled(false);
	}//GEN-LAST:event_newBActionPerformed
	
	private void editBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_editBActionPerformed
	{//GEN-HEADEREND:event_editBActionPerformed
		changeComponentState(true);
		newB.setEnabled(false);
		editB.setEnabled(false);
	}//GEN-LAST:event_editBActionPerformed

	private void deleteBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteBActionPerformed
		if(confirmDeletion("ruten"))
		{
			controller.delete(chosenOne.getRouteNo());
			clearComponents();
		}
	}//GEN-LAST:event_deleteBActionPerformed
	
	private void cancelBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelBActionPerformed
	{//GEN-HEADEREND:event_cancelBActionPerformed
		clearComponents();
		changeComponentState(false);
	}//GEN-LAST:event_cancelBActionPerformed
	
	private void saveBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_saveBActionPerformed
	{//GEN-HEADEREND:event_saveBActionPerformed
		Route theOne = getTheOne();

		if(chosenOne == null)
		{
			controller.insert(theOne);
		}
		else
		{
			controller.update(theOne, "routeNo = "+theOne.getRouteNo());
		}

		informSavingWentOk("Ruten");
		clearComponents();
		changeComponentState(false);
		newB.setEnabled(true);
	}//GEN-LAST:event_saveBActionPerformed
	
	private void searchBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_searchBActionPerformed
	{//GEN-HEADEREND:event_searchBActionPerformed
		tabbedPane.search(TabIndex.Routes, searchTF.getText());
	}//GEN-LAST:event_searchBActionPerformed

	private void showAllBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_showAllBActionPerformed
	{//GEN-HEADEREND:event_showAllBActionPerformed
		tabbedPane.search(TabIndex.Routes, "*");
	}//GEN-LAST:event_showAllBActionPerformed

	private void addLocationBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addLocationBActionPerformed
	{//GEN-HEADEREND:event_addLocationBActionPerformed
		if(picker == null)
		{
			picker = createPicker(TabIndex.Locations);
			picker.addWindowListener(this);
			picker.setVisible(true);
		}
	}//GEN-LAST:event_addLocationBActionPerformed

	private void removeLocationBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_removeLocationBActionPerformed
	{//GEN-HEADEREND:event_removeLocationBActionPerformed
		int locationNo = Integer.parseInt(locationTable.getValueAt(locationTable.getSelectedRow(), 0).toString());
		Location location = new LocationController().find(locationNo);
		removeLocation(location);
	}//GEN-LAST:event_removeLocationBActionPerformed
	
	private void showLocationBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_showLocationBActionPerformed
	{//GEN-HEADEREND:event_showLocationBActionPerformed
		int locationNo = Integer.parseInt(locationTable.getValueAt(locationTable.getSelectedRow(), 0).toString());
		tabbedPane.switchTabWithNo(TabIndex.Locations, locationNo);
	}//GEN-LAST:event_showLocationBActionPerformed

	private void addLocation(Location location)
	{
		Route theOne = getTheOne();

		theOne.addLocation(location);
		refreshTable(theOne.getLocations());
	}

	private void removeLocation(Location location)
	{
		Route theOne = getTheOne();

		theOne.removeLocation(location);
		refreshTable(theOne.getLocations());
	}

	private void refreshTable(ArrayList<Location> locations)
	{
		locationTable.setModel(new LocationTableModel(locations));
	}

	protected void changeComponentState(boolean state)
	{
		locationTable.setEnabled(state);
		addLocationB.setEnabled(state);
		cancelB.setEnabled(state);
		saveB.setEnabled(state);
		addLocationB.setEnabled(state);
		setUnsavedState(state);
	}

	protected void populateFields()
	{
		routeTF.setText(String.valueOf(chosenOne.getRouteNo()));
		totalTimeTF.setText(chosenOne.getTotalTimeToString());
		totalKmTF.setText(String.valueOf(chosenOne.getTotalDistance()));
		locationTable.setModel(new LocationTableModel(chosenOne.getLocations()));
		newB.setEnabled(true);
		editB.setEnabled(true);
		deleteB.setEnabled(true);
	}

	protected void clearComponents()
	{
		chosenOne = null;
		newOne = null;
		routeTF.setText("");
		totalTimeTF.setText("");
		totalKmTF.setText("");
		locationTable.setModel(new LocationTableModel());
		newB.setEnabled(true);
		editB.setEnabled(false);
		deleteB.setEnabled(false);
	}

	protected void showNo(int no)
	{
		chosenOne = controller.find(no);
		populateFields();
	}

	public void valueChanged(ListSelectionEvent e)
	{
		if(locationTable.getSelectedRowCount() == 1)
		{
			removeLocationB.setEnabled(true);
			showLocationB.setEnabled(true);
		}
		else
		{
			removeLocationB.setEnabled(false);
			showLocationB.setEnabled(false);
		}
	}

	public void windowClosing(WindowEvent e)
	{
		if(picker != null)
		{
			Location location = (Location)picker.getTheChosenOne();
			if(location != null)
			{
				this.addLocation(location);
			}
			this.releasePicker();
		}
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSeparator Sep2;
    private javax.swing.JSeparator Sep3;
    private javax.swing.JButton addLocationB;
    private javax.swing.JButton cancelB;
    private javax.swing.JButton deleteB;
    private javax.swing.JButton editB;
    private javax.swing.JScrollPane locationSP;
    private javax.swing.JTable locationTable;
    private javax.swing.JButton newB;
    private javax.swing.JButton removeLocationB;
    private javax.swing.JLabel routeNoL;
    private javax.swing.JTextField routeTF;
    private javax.swing.JButton saveB;
    private javax.swing.JButton searchB;
    private javax.swing.JTextField searchTF;
    private javax.swing.JSeparator sep1;
    private javax.swing.JButton showAllB;
    private javax.swing.JButton showLocationB;
    private javax.swing.JLabel totalKmL;
    private javax.swing.JTextField totalKmTF;
    private javax.swing.JLabel totalTimeL;
    private javax.swing.JTextField totalTimeTF;
    // End of variables declaration//GEN-END:variables
}